//
//  CTWebMenuView.m
//  CTWebViewDemo
//
//  Created by ST on 16/9/12.
//  Copyright © 2016年 CT. All rights reserved.
//

#import "CTWebMenuView.h"
#import "UIColor+ST.h"
#import "STCommon.h"
#import "UIDevice+ST.h"
#import "CTWebMenuCell.h"
#import "CTWebMenuModel.h"

NS_ASSUME_NONNULL_BEGIN

@interface CTWebMenuView()<UICollectionViewDelegate, UICollectionViewDataSource>
/** 1.固定的容器 */
@property(nonatomic, strong)UICollectionView *collectionView;
/** 2.自定义的容器 */
@property(nonatomic, strong)UICollectionView *collectionCustomView;
/** 3.固定的容器布局 */
@property(nonatomic, strong)UICollectionViewFlowLayout *flowLayout;
/** 4.自定义的容器布局 */
@property(nonatomic, strong)UICollectionViewFlowLayout *flowCustomLayout;
/** 5.取消按钮 */
@property(nonatomic, strong)UIButton *buttonCancel;
@end

NS_ASSUME_NONNULL_END


@implementation CTWebMenuView
#pragma mark - --- 1.init 生命周期 ---

static id _instance = nil;

+ (id)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}

+ (instancetype)sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
}

- (id)copyWithZone:(NSZone *)zone
{
    return _instance;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupDefault];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setupDefault];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupDefault];
    }
    return self;
}

- (void)setupDefault
{
    // 1.设置自身的属性
    self.bounds = [UIScreen mainScreen].bounds;
    self.layer.opacity = 0.0;
    [self addTarget:self action:@selector(remove) forControlEvents:UIControlEventTouchUpInside];
    
    // 2.设置颜色
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:(255.0 - 76)/255];
   
    // 3.添加子视图
    [self addSubview:self.collectionView];
    [self addSubview:self.collectionCustomView];
    [self addSubview:self.buttonCancel];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
}
#pragma mark - --- 2.delegate 视图委托 ---

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (collectionView == self.collectionView) {
        return self.arraySystem.count;
    }else {
        return 100;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (collectionView == self.collectionView) {
        CTWebMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CTWebMenuCellId forIndexPath:indexPath];
        cell.model = self.arraySystem[indexPath.item];
//        cell.text = @(indexPath.row).stringValue;
//        cell.imageName = @"ui_webview_collect_n";
        return cell;
    }else {
        CTWebMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CTWebMenuCellId forIndexPath:indexPath];
        cell.text = @(indexPath.row).stringValue;
        cell.imageName = @"ui_webview_collect";
        return cell;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == self.collectionCustomView) {
         NSLog(@"%s %ld", __FUNCTION__, (long)indexPath.item);
    }
}

#pragma mark - --- 3.event response 事件相应 ---

#pragma mark - --- 4.private methods 私有方法 ---
- (void)show
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [self setCenter:[UIApplication sharedApplication].keyWindow.center];
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];

        CGRect frameContent =  self.collectionView.frame;
        frameContent.origin.y -= self.collectionView.frame.size.height;
        [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            [self.layer setOpacity:1.0];
//            self.collectionView.frame = frameContent;
        } completion:^(BOOL finished) {
        }];
}

- (void)remove
{

        CGRect frameContent =  self.collectionView.frame;
        frameContent.origin.y += self.collectionView.frame.size.height;
        [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            [self.layer setOpacity:0.0];
//            self.collectionView.frame = frameContent;
        } completion:^(BOOL finished) {
            [self.collectionView reloadData];
            [self.collectionCustomView reloadData];
//            self.collectionView indexPathsForSelectedItems
//            for (NSIndexPath *path in self.collectionView.indexPathsForSelectedItems) {
//                self.collectionView
//            }
            [self removeFromSuperview];
        }];

}
#pragma mark - --- 5.setters 属性 ---

#pragma mark - --- 6.getters 属性 —--

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, SCREENHEIGHT-HCTWebMenuCell-49, SCREENWIDTH, HCTWebMenuCell) collectionViewLayout:self.flowLayout];
        [_collectionView setDataSource:self];
        [_collectionView setDelegate:self];
        [_collectionView setBackgroundColor:[UIColor whiteColor]];
//        [_collectionView setBackgroundColor:[UIColor colorWithRed:242.0/255 green:242.0/255 blue:242.0/255 alpha:1.0]];
        [_collectionView registerClass:CTWebMenuCell.class forCellWithReuseIdentifier:CTWebMenuCellId];
        [_collectionView setShowsVerticalScrollIndicator:NO];
        [_collectionView setShowsHorizontalScrollIndicator:NO];
        [_collectionView setPagingEnabled:YES];
    }
    return _collectionView;
}

- (UICollectionView *)collectionCustomView
{
    if (!_collectionCustomView) {
        _collectionCustomView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, SCREENHEIGHT-HCTWebMenuCell*2-49, SCREENWIDTH, HCTWebMenuCell) collectionViewLayout:self.flowCustomLayout];
        [_collectionCustomView setDataSource:self];
        [_collectionCustomView setDelegate:self];
        [_collectionCustomView setBackgroundColor:[UIColor whiteColor]];
//        [_collectionCustomView setBackgroundColor:[UIColor colorWithRed:242.0/255 green:242.0/255 blue:242.0/255 alpha:1.0]];
        [_collectionCustomView registerClass:CTWebMenuCell.class forCellWithReuseIdentifier:CTWebMenuCellId];
        [_collectionCustomView setShowsVerticalScrollIndicator:NO];
        [_collectionCustomView setShowsHorizontalScrollIndicator:NO];
        [_collectionCustomView setPagingEnabled:YES];
        
    }
    return _collectionCustomView;
}

- (UICollectionViewFlowLayout *)flowLayout
{
    if (!_flowLayout) {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        CGFloat widthItem = SCREENWIDTH/4;
        CGFloat heightItem = HCTWebMenuCell;
        _flowLayout.itemSize = CGSizeMake(widthItem, heightItem);
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _flowLayout.minimumLineSpacing = 0;
    }
    return _flowLayout;
}

- (UICollectionViewFlowLayout *)flowCustomLayout
{
    if (!_flowCustomLayout) {
        _flowCustomLayout = [[UICollectionViewFlowLayout alloc] init];
        CGFloat widthItem = SCREENWIDTH/4;
        CGFloat heightItem = HCTWebMenuCell;
        _flowCustomLayout.itemSize = CGSizeMake(widthItem, heightItem);
        _flowCustomLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _flowCustomLayout.minimumLineSpacing = 0;
    }
    return _flowCustomLayout;
}

- (UIButton *)buttonCancel
{
    if (!_buttonCancel) {
        _buttonCancel = [[UIButton alloc]initWithFrame:CGRectMake(0, SCREENHEIGHT - 49, SCREENWIDTH, 49)];
        [_buttonCancel setTitle:@"取消" forState:UIControlStateNormal];
        [_buttonCancel setTitleColor:[UIColor colorWithRed:95.0/255 green:95.0/255 blue:95.0/255 alpha:1.0] forState:UIControlStateNormal];
        [_buttonCancel setBackgroundColor:[UIColor whiteColor]];
//        [_buttonCancel setBackgroundColor:[UIColor colorWithRed:242.0/255 green:242.0/255 blue:242.0/255 alpha:1.0]];
        [_buttonCancel.titleLabel setFont:[UIFont systemFontOfSize:17]];
        [_buttonCancel addTarget:self action:@selector(remove) forControlEvents:UIControlEventTouchUpInside];
    }
    return _buttonCancel;
}

- (NSMutableArray<CTWebMenuModel *> *)arraySystem
{
    if (!_arraySystem) {
        _arraySystem = @[].mutableCopy;
        
        NSArray *arrayName = @[@"复制链接", @"刷新页面", @"字体调整", @"投诉"];
        NSArray *arrayImage = @[@"ui_webview_menu_01",@"ui_webview_menu_02",@"ui_webview_menu_03",@"ui_webview_menu_04"];
        
        for (NSInteger number = 0; number < 4; number++) {
            CTWebMenuModel *model = [[CTWebMenuModel alloc]init];
            model.title = arrayName[number];
            model.imageName = arrayImage[number];
            model.functionName = @"remove";
            [_arraySystem addObject:model];
        }
    }
    return _arraySystem;
}
@end


